﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class User_Employer_ProjectControl : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LblStatus.Text = "";
        DropDownListIsDone.Items[1].Attributes["style"] = "color:red;";
        DropDownListIsDone.Items[0].Attributes["style"] = "color:green;";
    }
    public void SelectContract_click(object sender)
    {
        Session["contractid"] = SelectContract1.Contractid.ToString();
        LblDate.Text = SelectContract1.Date;
        lblvilname.Text = SelectContract1.VillageName;
        LblConNum.Text = SelectContract1.ContractNumber;
        MultiView1.ActiveViewIndex = 1;
        SubSonic.Query q = DataAccess.TblTask.CreateQuery();
        q.QueryType = SubSonic.QueryType.Select;
        q.AddWhere(DataAccess.TblTask.Columns.ContractId, SelectContract1.Contractid);
        q.AddWhere(DataAccess.TblTask.Columns.IsDonei, bool.TrueString);
        IDataReader dr = q.ExecuteReader();
        int i = 0;
        if (dr != null)
        {
            while (dr.Read())
            {
                i += 1;
            }
        }
        dr.Close();

        if (SelectContract1.Role == "Advisor")
        {

            Session["Role"] = "Advisor";
            SubSonic.Query qad = DataAccess.TblStepsAdvisor.CreateQuery();
            qad.QueryType = SubSonic.QueryType.Select;
            qad.BETWEEN_VALUES("Priority", 0, i + 1);
            qad.OrderBy = SubSonic.OrderBy.Asc("Priority");

            DataList1.DataSource = qad.ExecuteReader();
            DataList1.DataBind();
        }
        if (SelectContract1.Role == "Surveyor")
        {
            Session["Role"] = "Surveyor";
            SubSonic.Query qad = DataAccess.TblStepsSurveyor.CreateQuery();
            qad.QueryType = SubSonic.QueryType.Select;
            qad.BETWEEN_VALUES("Priority", 0, i + 1);
            qad.OrderBy = SubSonic.OrderBy.Asc("Priority");

            DataList1.DataSource = qad.ExecuteReader();
            DataList1.DataBind();
        }

    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DataList1.SelectedItem.ItemIndex < 0)
        {
            Session["stepid"] = null;
        }
        else
        {
            Session["stepid"] = DataList1.SelectedValue;
        }
    }
    
    protected void ButtonRegister_Click(object sender, EventArgs e)
    {
        try
        {
            bool control = false;
              Guid contractid = new Guid(Session["contractid"].ToString()); 
        DataAccess.TblContract con=new DataAccess.TblContract();
        con = DataAccess.TblContract.FetchByID(new Guid( Session["contractid"].ToString()));
            if (Session["stepid"] != null || Session["contractid"] != null)
            {
                SubSonic.Query qad = DataAccess.TblTask.CreateQuery();
                qad.QueryType = SubSonic.QueryType.Select;
                if (Session["Role"] .ToString()== "Advisor")
                qad.AddWhere("AdvisorStepId", SubSonic.Comparison.Equals, Session["stepid"].ToString());
                if (Session["Role"].ToString() == "Surveyor")
                qad.AddWhere("SurveyorStepId", SubSonic.Comparison.Equals, Session["stepid"].ToString());

                qad.AddWhere("ContractId", SubSonic.Comparison.Equals, Session["contractid"].ToString());
                qad.OrderBy = SubSonic.OrderBy.Asc("Date");
                IDataReader dr = qad.ExecuteReader();
                dr.Read();
                Guid taskid = new Guid(dr["ID"].ToString());
                dr.Close();
                SubSonic.Query qad1 = DataAccess.TblTask.CreateQuery();
                qad1.QueryType = SubSonic.QueryType.Update;
              
                if (DropDownListIsDone.SelectedItem.Value=="0")
                {
                qad1.AddUpdateSetting(DataAccess.TblTask.Columns.IsDonei, bool.FalseString);
                    control=false;
                }
                if (DropDownListIsDone.SelectedItem.Value == "1")
                {
                    control=true;
                    qad1.AddUpdateSetting(DataAccess.TblTask.Columns.IsDonei, bool.TrueString);
                }
                qad1.AddUpdateSetting(DataAccess.TblTask.Columns.Status, TxtStatus.Text);
                qad1.AddWhere(DataAccess.TblTask.Columns.Id, taskid);
                qad1.Execute();
                string strreason = "";
                if (TxtStatus.Text.ToString().Trim() != "")
                    strreason = TxtStatus.Text;
                else
                    strreason = ".....";
                Business.Notification n = new Business.Notification();
                if (Session["Role"].ToString() == "Advisor")
                
                    n.SaveInnerNotific(new Guid(Session["contractid"].ToString()), new Guid(Session["stepid"].ToString()), true, con.UserId,control,strreason);
                
                if (Session["Role"].ToString() == "Surveyor")

                    n.SaveInnerNotific(new Guid(Session["contractid"].ToString()), new Guid(Session["stepid"].ToString()), true, con.UserId, control, strreason);
                if(n.findbonyadshahrestan(new Guid(Session["contractid"].ToString()))!=Guid.Empty)
                    n.SaveInnerNotific(new Guid(Session["contractid"].ToString()), new Guid(Session["stepid"].ToString()), true, n.findbonyadshahrestan(new Guid(Session["contractid"].ToString())), control, strreason);
                n.SaveInnerNotific(new Guid(Session["contractid"].ToString()), new Guid(Session["stepid"].ToString()), true,new Guid( con.FourthFactorUserId.ToString()), control, strreason);

                List<string> userids = new List<string>();
                userids.Add(con.FourthFactorUserId.ToString());
                userids.Add(con.UserId.ToString());
                userids.Add(n.findbonyadshahrestan(contractid).ToString());
                string[] mobiles = n.FindMobile(userids);
                SendSmS(mobiles);
                LblStatus.Text = " <script>alert('.ثبت با موفقیت انجام شد')</script>";
            }
            else
            {
                LblStatus.Text = " <script>alert('.ابتدا مرحله را انتخاب کنید')</script>";
            }
        }
        catch
        {
            LblStatus.Text = " <script>alert('.ثبت با خطا مواجه شد')</script>";
        }
    }
    public void SendSmS(string[] D)
    {
        com.sobhsms.www.Service1 s = new com.sobhsms.www.Service1();

        string str = "لطفاهرچه سریعتربه وبسایت www.bonyadmaskanfars.ir/fpm مراجعه بفرمایید.";
        str = str.Replace("@", Environment.NewLine);

        if (D.Length > 0)
        {
            long[] r = s.SendPackageMessageConcFast(D, str, "Pwebsrvbnydmskn90");
        }
        //Response.Write(r[0].ToString());
    }
}